package com.shensi.adminCollect.service.station.impl;


import com.github.pagehelper.PageHelper;
import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;
import com.shensi.adminCollect.domain.station.CommodityManagement;
import com.shensi.adminCollect.mapper.station.CommodityManagementMapper;
import com.shensi.adminCollect.service.station.CommodityManagementService;
import com.shensi.adminCollect.service.station.INonOilCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (CommodityManagement)表服务实现类
 *
 * @author makejava
 * @since 2023-08-16 11:26:58
 */
@Service("commodityManagementService")
public class CommodityManagementServiceImpl implements CommodityManagementService {
    @Resource
    private CommodityManagementMapper commodityManagementMapper;

    @Autowired
    private INonOilCategoryService nonOilCategoryService;
    /**
     * 通过ID查询单条数据
     *
     * @param cmId 主键
     * @return 实例对象
     */
    @Override
    public CommodityManagement queryById(Long cmId) {
        return this.commodityManagementMapper.queryById(cmId);
    }

    /**
     * 分页查询
     *
     * @param commodityManagement 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<CommodityManagement> queryByPage(CommodityManagement commodityManagement, PageRequest pageRequest) {
        long total = this.commodityManagementMapper.count(commodityManagement);
        return new PageImpl<>(this.commodityManagementMapper.queryAllByLimit(commodityManagement, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param commodityManagement 实例对象
     * @return 实例对象
     */
    @Override
    public CommodityManagement insert(CommodityManagement commodityManagement) {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        commodityManagement.setCreateUser(currentUser.getUser().getUserId());
        commodityManagement.setCreateTime(new Date());
        this.commodityManagementMapper.insert(commodityManagement);
        return commodityManagement;
    }

    /**
     * 修改数据
     *
     * @param commodityManagement 实例对象
     * @return 实例对象
     */
    @Override
    public CommodityManagement update(CommodityManagement commodityManagement) {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        commodityManagement.setUpdateUser(currentUser.getUser().getUserId());
        commodityManagement.setUpdateTime(new Date());
        this.commodityManagementMapper.update(commodityManagement);
        return this.queryById(commodityManagement.getCmId());
    }

    /**
     * 通过主键删除数据
     *
     * @param cmId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long cmId) {
        return this.commodityManagementMapper.deleteById(cmId) > 0;
    }

    @Override
    public int delete(List<Long> ids) {
        if(ids!=null&&ids.size()>0){
            return commodityManagementMapper.deleteByIds(ids);
        }
        return 0;
    }

    @Override
    public List<CommodityManagement> query(CommodityManagement commodityManagement) {
        Long categoryId = commodityManagement.getCategoryId();
        if(categoryId!=null){
            List<Long> childIds = nonOilCategoryService.getChildIds(categoryId);
            childIds.add(categoryId);
            commodityManagement.setCategoryIds(childIds);
        }
        PageHelper.startPage(commodityManagement.getPageNum(), commodityManagement.getPageSize(), commodityManagement.getOrderBy());
        List<CommodityManagement> query = commodityManagementMapper.query(commodityManagement);
        PageHelper.clearPage();

//        List<NonOilCategory> list = nonOilCategoryService.selectAll();
//        for (CommodityManagement management : query) {
//            management.setCategoryName(nonOilCategoryService.queryCategoryName(management.getCategoryId(),list));
//        }
        return query;
    }

    @Override
    public Map<String, String> getStatisticsData(CommodityManagement commodityManagement) {
        List<Map<String, String>> statisticsData = commodityManagementMapper.getStatisticsData(commodityManagement);
        Map<String,String> map=new HashMap<>();
        for (Map<String, String> statisticsDatum : statisticsData) {
            map.put(statisticsDatum.get("status"),statisticsDatum.get("count"));
        }
        return map;
    }
}
